﻿Imports System.IO
Imports System.Drawing
Imports System.Data.SqlClient


Partial Class photoalbums_admin_editImage
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim imageAdapter As New dsImagesTableAdapters.ImagesTableAdapter()
        Dim ImgID As Integer = Request.QueryString("ImgID")

        repImage.DataSource = imageAdapter.GetImageByID(ImgID)
        repImage.DataBind()

    End Sub

    Public Function ResizeImage(ByVal ImgID As Integer) As String
        Dim connString As String = ConfigurationManager.ConnectionStrings("Henson Family").ConnectionString
        Dim conn As New SqlConnection(connString)
        Dim reader As SqlDataReader

        conn.Open()

        Dim sql As String = "SELECT ImgID, Title, Caption, Filepath, Private FROM Images WHERE ImgID = @ImgID"
        Dim comm As New SqlCommand(sql, conn)

        comm.Parameters.AddWithValue("@ImgID", ImgID)

        reader = comm.ExecuteReader()

        Dim imgHeight As Integer
        Dim imgWidth As Integer
        Dim sizeRatio As Double
        Dim maxWidth As String = "700"
        Dim imageOutput As String
        Dim filePath As String

        If reader.Read() Then
            filePath = Server.MapPath("/img/imgGallery/")

            For Each Image In Directory.GetFiles(filePath, reader("Filepath"))
                Dim currentImage As System.Drawing.Image = System.Drawing.Image.FromFile(Image)

                imgHeight = currentImage.Height
                imgWidth = currentImage.Width

                If imgWidth > maxWidth Then
                    sizeRatio = maxWidth / imgWidth
                    imgWidth = maxWidth
                    imgHeight = imgHeight * sizeRatio
                End If
            Next
        End If

        imageOutput = "<img src=""/img/imgGallery/" & reader("Filepath") & """ height=""" & imgHeight & """ width=""" & imgWidth & """ alt="""" />"
        Return imageOutput
    End Function
End Class
